<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Db;
use \think\Session;
// use app\admin\model\Employ as EmployModel;//管理员模型
use app\admin\controller\Permissions;
class LawyerCase extends Permissions
{
    public function index()
    {

        $name=trim($this->request->Post('name'));
        $state=$this->request->Post('state');
        if($name !='') $where['name']= ['like','%' . $name . '%'];
        if($state !='') $where['state']= ['=',$state];
		$join=[["lawyer l","c.lawyer_id=l.id","LEFT"],["admin a","a.id=c.add_id","LEFT"]];

        $case = empty($where)?Db::name('lawyer_case')->alias("c")->join($join)->field(["c.*,a.name","l.name as l_name"])->order("id","desc")->paginate(10): Db::name('lawyer_case')->alias("c")->join($join)->where($where)->order("id","desc")->paginate(10,true,['query'=>request()->Post()]);
        $this->assign('case',$case);
        // var_dump($news);
        return $this->fetch();
    }

    /**
     * 添加产品
     */
    public function add(){
		
        //判断是不是操作数据库  
        if($this->request->isPost()){

            $post=$this->request->Post();
            $id=$this->request->Post("id");
            //验证  唯一规则： 表名，字段名，排除主键值，主键名

                $validate = new \think\Validate([
                    ['title', 'require', '案例标题必选!'],
					['subtitle', 'require', '案例副标题必选!'],
                    ['content', 'require', '案例必填!'],
                    ['lawyer_id', 'require', '律师必填!'],
                ]);
            // 验证部分数据合法性
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }
			if($id!=''){
				$old_img=Db::name("lawyer_case")->where('id',$id)->value('img');
			}
					  // 获取表单上传文件 例如上传了001.jpg
			$file = Request()->file("img");
				if(!empty($file)){
					$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/lawyer_case');
					if($info){
						$img =$info->getSaveName();
						@unlink($_SERVER['DOCUMENT_ROOT']."cheeryou/public/uploads/lawyer_case/".$old_img);
					}else{
						echo $file->getError();
					}
				}else{
					$img=$old_img;
				}
            $data=[
                    'add_id'=>Session::get('admin'),
                    'content'=>$post['content'],
					'subtitle'=>$post['subtitle'],
					'title'=>$post['title'],
					'type'=>$post['type'],
                    'lawyer_id'=>$post['lawyer_id'],
                    'state'=>0,
					 'img'=>$img,
                    "create_time"=>time()
                ];
			// return $data;
            if($id==null){       
                $res=Db::name('lawyer_case')->insert($data);
				// return $res;
                if($res==false) return $this->error('保存失败');   
            }else{
                $data1=[
                     'add_id'=>Session::get('admin'),
                    'content'=>$post['content'],
					'subtitle'=>$post['subtitle'],
                    'title'=>$post['title'],
					'type'=>$post['type'],
                    'lawyer_id'=>$post['lawyer_id'],
                    'state'=>0,
					'img'=>$img,
                ];
                $res = Db::name('lawyer_case')->where('id',$id)->update($data1);
                if($res==false) return $this->error('修改失败');     
            }
            return $this->success("保存成功",'admin/lawyer_case/index');
        }elseif($this->request->has('id')){
			$type=Db::name("case_type")->where("pid","neq","0")->select();
			$this->assign("type",$type);
			$lawyer=Db::name("lawyer")->order("id","desc")->select();
			$this->assign("lawyer",$lawyer);
            //获取对应数据id
            $id=$this->request->param("id");
            $case=Db::name("lawyer_case")->where("id",$id)->find();
            // var_dump($friendly);
            $this->assign("case",$case);
			// var_dump($case);
            return $this->fetch();
        }else{
			$type=Db::name("case_type")->where("pid","neq","0")->select();
			$this->assign("type",$type);
			$lawyer=Db::name("lawyer")->order("id","desc")->select();
			$this->assign("lawyer",$lawyer);
			// var_dump($lawyer);
            return $this->fetch();
        }
    }
	
	    public function menulist($menu,$id=0,$level=0){
        
        static $menus = array();
        $size = count($menus)-1;
        foreach ($menu as $value) {
            if ($value['pid']==$id) {
                $value['level'] = $level+1;
                if($level == 0)
                {
                    $value['str'] = str_repeat('',$value['level']);
                    $menus[] = $value;
                }
                elseif($level == 2)
                {
                    $value['str'] = '&emsp;&emsp;&emsp;&emsp;'.'└ ';
                    $menus[$size]['list'][] = $value;
                }
                elseif($level == 3)
                {
                    $value['str'] = '&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;'.'└ ';
                    $menus[$size]['list'][] = $value;
                }
                else
                {
                    $value['str'] = '&emsp;&emsp;'.'└ ';
                    $menus[$size]['list'][] = $value;
                }
                
                $this->menulist($menu,$value['id'],$value['level']);
            }
        }
        return $menus;
    }

     /**
     * 案例删除
     * @return [type] [description]
     */
    public function delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('lawyer_case')->where('id',$id)->delete()) {
                return 1;
            } else {
                return 0;
            }
        }
    }

    /**
     * 后台审核律师案例
     */
    public function shenhe_index(){
        $lawyer_name=trim($this->request->Post('lawyer_name'));
        if($lawyer_name !='') $where['l.name']= ['like','%' . $lawyer_name . '%'];
        $a_name=trim($this->request->Post('a_name'));
        if($a_name !='') $where['a.name']= ['like','%' . $a_name . '%'];
        
        $join=[["lawyer l","c.lawyer_id=l.id","LEFT"],["admin a","a.id=c.add_id","LEFT"]];
        $where["state"]=["=","0"];
        $case = Db::name('lawyer_case')->alias("c")->join($join)->field(["c.*,a.name","l.name as l_name"])->where($where)->order("id","desc")->paginate(10,false,['query'=>request()->Post()]);
        $this->assign('case',$case);
        return $this->fetch();
    }

    /**
     * 审核
     */
    public function shenhe(){

        if(Request()->IsPost()){
            $post=Request()->post();
            $res=Db::name("lawyer_case")->where("id",$post['id'])->update(["state"=>$post['state']]);
            if($res){
                return $this->success("信息保存成功","lawyer_case/shenhe_index");
            }else{
                return $this->error("信息保存失败");
            }
        }else{
            $id=Request()->param("id");
            //获取对应数据id
            $case=Db::name("lawyer_case")->alias("c")->join([["lawyer l","l.id=c.lawyer_id","LEFT"],["admin a","a.id=c.add_id","LEFT"]])->field(["c.*","a.name as a_name","l.name as l_name"])->where("c.id",$id)->find();
            // var_dump($friendly);
            $this->assign("case",$case);
            return $this->fetch();
        }
    }
	

}
